package com.bkhc.umc.api;

import com.bkhc.common.annotation.ApiOperation;
import com.bkhc.common.annotation.ApiPackage;
import com.bkhc.common.annotation.AuditLog;
import com.bkhc.common.enu.OperationEnum;
import com.bkhc.common.util.CommonUtil;
import com.bkhc.common.util.response.ResultJson;
import com.bkhc.common.vo.UserBean;
import com.bkhc.common.vo.UserVo;
import com.bkhc.umc.entity.User;
import com.bkhc.umc.entity.UserRole;
import com.bkhc.umc.service.UserSerivce;
import com.bkhc.umc.vo.UserInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author pcq
 * @date 2021/6/20 17:57
 */
@RestController
@RequestMapping("/user")
@Api("后台管理服务接口")
@ApiPackage(code="userService",name="后台用户服务接口",description = "后台管理员相关维护操作接口")
public class UserController extends CommonController{

    @Autowired
    private UserSerivce userSerivce;
    @ApiOperation(code="queryUser",name="查询后台用户",description = "查询后台所有用户")
    @AuditLog(object="User",operation = "查询用户",type = OperationEnum.query)
    @RequestMapping(path = "/query",method = RequestMethod.POST)
    public ResultJson<?> getAllUser(@RequestBody(required=false) UserVo userVo, @RequestParam(name="pageIndex") int pageIndex, @RequestParam(name="pageSize") int pageSize) {
        ResultJson<?> res = userSerivce.queryUser(userVo, pageIndex, pageSize);
        return res;
    }

    @ApiOperation(code="addUser",name="增加后台",description = "增加后台用户")
    @AuditLog(object="User",operation = "增加用户",type = OperationEnum.add)
    @RequestMapping(path = "/add",method = RequestMethod.POST)
    public ResultJson<?> addUser(@RequestBody(required=false) User user) {
        ResultJson<?> res = userSerivce.addUser(user);
        return res;
    }

    @ApiOperation(code="updateUser",name="后台",description = "修改后台用户")
    @AuditLog(object="User",operation = "修改用户",type = OperationEnum.update)
    @RequestMapping(path = "/update",method = RequestMethod.POST)
    public ResultJson<?> updateUser(@RequestBody(required=false) User user) {
        ResultJson<?> res = userSerivce.updateUser(user);
        return res;
    }

    @ApiOperation(code="deleteUser",name="后台",description = "删除后台用户")
    @AuditLog(object="User",operation = "删除用户",type = OperationEnum.delete)
    @RequestMapping(path = "/del",method = RequestMethod.POST)
    public ResultJson<?> deleteUser(@RequestBody(required=false) User user) {
        ResultJson<?> res = userSerivce.delete(user);
        return res;
    }

    @ApiOperation(code="queryUserRole",name="查询用户角色",description = "查询后台用户角色")
    @AuditLog(object="User",operation = "查询用户",type = OperationEnum.query)
    @RequestMapping(path = "/queryUserRole/{userId}",method = RequestMethod.POST)
    public ResultJson<?> getUserRole(@PathVariable(name="userId") String userId,@RequestParam(name="status") String status) {
        ResultJson<?> res = userSerivce.getUserRole(userId,status);
        return res;
    }

    @ApiOperation(code="queryUserRoleDetail",name="查询用户角色详情",description = "查询后台用户角色详情")
    @AuditLog(object="UserRole",operation = "查询用户",type = OperationEnum.query)
    @RequestMapping(path = "/queryUserRoleDetail/{userId}",method = RequestMethod.POST)
    public ResultJson<?> queryUserRoleDetail(@PathVariable(name="userId") String userId) {
        ResultJson<?> res = userSerivce.queryUserRoleDetail(userId);
        return res;
    }

    @ApiOperation(code="updateUserRoleDetail",name="更新用户角色详情",description = "更新后台用户角色详情")
    @AuditLog(object="UserRole",operation = "更新用户角色详情",type = OperationEnum.query)
    @RequestMapping(path = "/updateUserRoleDetail/{userId}",method = RequestMethod.POST)
    public ResultJson<?> updateUserRoleDetail(@RequestBody(required = false) UserRole userRole) {
        ResultJson<?> res = userSerivce.updateUserRoleDetail(userRole);
        return res;
    }

    @ApiOperation(code="register",name="注册",description = "注册")
    @AuditLog(object="UserRole",operation = "注册",type = OperationEnum.query)
    @RequestMapping(path = "/register",method = RequestMethod.POST)
    public ResultJson<?> register(@RequestBody(required = false) UserInfo userinfo) {
        ResultJson<?> res = userSerivce.register(userinfo);
        return res;
    }

    @ApiOperation(code="getUserByUsernameFuzzy",name="查询后台用户",description = "查询后台用户")
    @AuditLog(object="User",operation = "查询用户",type = OperationEnum.query)
    @RequestMapping(path = "/getUserByUsernameFuzzy",method = RequestMethod.POST)
    public ResultJson<List<User>> getUserByUsernameFuzzy(@RequestParam(name="username") String username) {
        List<User> users= userSerivce.getUserByUsernameFuzzy(username);
        ResultJson<List<User>> resultJson = ResultJson.success();
        resultJson.setData(users);
        return resultJson;
    }

    @ApiOperation(code="getUserByUsername",name="根据用户名查询用户资料",description = "根据用户名查询用户资料")
    @AuditLog(object="User",operation = "根据用户名查询用户资料",type = OperationEnum.query)
    @RequestMapping(path = "/getUserByUsername",method = RequestMethod.POST)
    public ResultJson<User> getUserByUsername(@RequestParam(name="username") String username) {
        User user= userSerivce.getUserByUsername(username);
        ResultJson<User> resultJson = ResultJson.success();
        resultJson.setData(user);
        return resultJson;
    }

    @ApiOperation(code="getCurrentUser",name="查询当前登录用户",description = "查询当前登录用户")
    @AuditLog(object="User",operation = "查询当前登录用户",type = OperationEnum.query)
    @RequestMapping(path = "/getCurrentUser",method = RequestMethod.POST)
    public ResultJson<User> getCurrentUser() {
        User user= userSerivce.getCurrentUser();
        ResultJson<User> resultJson = ResultJson.success();
        resultJson.setData(user);
        return resultJson;
    }

    @ApiOperation(code="currentUseRoleDetail",name="查询登录用户角色详情",description = "查询登录用户角色详情")
    @AuditLog(object="UserRole",operation = "查询登录用户角色详情",type = OperationEnum.query)
    @RequestMapping(path = "/currentUseRoleDetail",method = RequestMethod.POST)
    public ResultJson<?> querycurrentUseRoleDetail() {
        UserBean u = CommonUtil.getCrruentUser();
        ResultJson<?> res = userSerivce.queryUserRoleDetail(u.getUserId());
        return res;
    }

    @ApiOperation(code="updatePassword",name="查询登录用户角色详情",description = "查询登录用户角色详情",type="common")
    @AuditLog(object="User",operation = "查询登录用户角色详情",type = OperationEnum.query)
    @RequestMapping(path = "/updatePassword",method = RequestMethod.POST)
    public ResultJson<?> updatePassword(@RequestParam(name = "oldPassword")String oldPassword,@RequestParam(name = "newPassword")String newPassword) {
        UserBean u = CommonUtil.getCrruentUser();
        ResultJson<?> res = userSerivce.updatePassword(u,oldPassword,newPassword);
        return res;
    }

    @ApiOperation(code="addUPCollection",name="增加用户产品收藏",description = "增加用户产品收藏",type="common")
    @AuditLog(object="User",operation = "增加用户产品收藏",type = OperationEnum.query)
    @RequestMapping(path = "/addUPCollection",method = RequestMethod.POST)
    public ResultJson<?> addUPCollection(@RequestParam(name = "productCode")String productCode) {

        ResultJson<?> res = userSerivce.addUPCollection(productCode);
        return res;
    }

    @ApiOperation(code="deleteUPCollection",name="取消用户产品收藏",description = "取消用户产品收藏",type="common")
    @AuditLog(object="User",operation = "取消用户产品收藏",type = OperationEnum.query)
    @RequestMapping(path = "/deleteUPCollection",method = RequestMethod.POST)
    public ResultJson<?> deleteUPCollection(@RequestParam(name = "productCode")String productCode) {

        ResultJson<?> res = userSerivce.deleteUPCollection(productCode);
        return res;
    }
}
